package com.zhuhjay.boot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zhuhjay.constant.MessageConstant;
import com.zhuhjay.domain.http.Result;
import com.zhuhjay.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuhjay
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("/username")
    public Result getUsername(){
        try{
            // 通过SpringSecurity获取用户信息
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }


}
